<?php
class MymemberAction extends CommonAction {

	function index(){
		header("Content-type:text/html;charset=utf-8");
		//get userid
		$myId         = $_SESSION[C('USER_AUTH_KEY')];
		$isAdmin   = $_SESSION[C('ADMIN_AUTH_KEY')];
		if($isAdmin){
			echo "You are Administrator user, please wait for your function complete, have a Good day!";
			//here display all salers to link their members;
			return;
		}
		$Role_user = M('Role_user');
		$isSaler = $Role_user->where("user_id=$myId AND role_id=10")->find();
		if( !$isSaler ){
			//teacher's members
			$isTeach = $Role_user->where("user_id=$myId AND role_id=9")->find();
			if($isTeach){
				$Consumelog = M('Consumelog');
				$myMemberId   = $Consumelog->field('memberId')->where('teachId='.$myId)->select();
				foreach($myMemberId as $mmid){
					$myMemberIds .= $mmid['memberId'].',';
				}
				$myMemberIds .= '0';
				$Member = M('Member');
				$count      = $Member->where('id in ('.$myMemberIds.')')->count();
				if ($count > 0) {
					import ( "@.ORG.Page" );
					$p = new Page ( $count, C('PAGE_SIZE') );
					$voList = $Member
					          ->field('dudu_member.*,dudu_card.salerName,dudu_card.typeName,dudu_card.money,dudu_card.startDate,dudu_card.endDate')
							  ->join("dudu_card on cardId=dudu_card.id")
							  ->where('dudu_member.id in ('.$myMemberIds.')')
							  ->limit($p->firstRow . ',' . $p->listRows)->findAll ( );
					$this->assign('list', $voList);
					$this->assign('page',$p->show());
					$this->display();
					return;
				}
			}
			echo "Error~~~,OH,I'm NOT a Saler!!! Try other options";
			return;
		}

		/**
		 * Get my member
		 *
		 * 1st:   get all my cards;
		 * 2nd: get member informations;
		 * 3rd:  show display.
		 *
		 */

		$Card = M ('Card');
		$count = $Card->where("salerId=$myId")->count('id');
		if( $count >0 ){
			import ( "@.ORG.Page" );
			$p = new Page ( $count, C('PAGE_SIZE') );
			$myCard = $Card->where("salerId=$myId")->order("memberId DESC")->limit($p->firstRow . ',' . $p->listRows)->findAll () ;
			$myMember = array();
			$Member = M('Member');
			foreach($myCard as $key=> $mc){
				$member = $Member->where("id = $mc[memberId]")->find();
				$myMember[] = $member;
				$myCard[$key]['account'] = $member['account'];
				$myCard[$key]['trueName'] = $member['trueName'];
				$myCard[$key]['telephone'] = $member['telephone'];
				$myCard[$key]['createTime'] = $member['createTime'];
			}
			$this->assign('list', $myCard);
			$this->assign('page',$p->show());
			$this->display();
		}
	}
}
?>